﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VTV.TaskManagementSystem.DataAccess
{
    public partial class Team : IConvertToLinqEntity<Team>, ISetting<Entities.Team>
    {
        public string ErrorMessage { get; set; }

        public Team Convert(object baseEntity)
        {
            Team result = null;
            try
            {
                if (baseEntity != null)
                {
                    var entity = (Entities.Team)baseEntity;

                    return result = new Team
                    {
                        Name = String.IsNullOrEmpty(entity.Name) ? null : entity.Name,
                        TeamID = entity.TeamID,
                        Note = String.IsNullOrEmpty(entity.Note) ? null : entity.Note
                    };
                }
                else
                {
                    ErrorMessage = Common.Constant.ErrorMessage.SOURCE_IS_NULL;
                }
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;
            }

            return result;
        }

        public bool AddNew(Entities.Team source)
        {
            try
            {
                var newSource = this.Convert(source);

                using (var data = new DataBaseDataContext())
                {
                    data.Teams.InsertOnSubmit(newSource);
                    data.SubmitChanges();
                }
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;

                return false;
            }

            return true;
        }

        public bool Delete(Entities.Team source)
        {
            try
            {
                var newSource = this.Convert(source);

                if (newSource != null)
                {
                    using (var data = new DataBaseDataContext())
                    {
                        data.Teams.Attach(newSource);
                        data.Teams.DeleteOnSubmit(newSource);
                        data.SubmitChanges();

                        return true;
                    }
                }
                else
                {
                    return false;
                }
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;

                return false;
            }
        }

        public bool Update(Entities.Team source)
        {
            try
            {
                using (var data = new DataBaseDataContext())
                {
                    var team = data.Teams.FirstOrDefault(p => p.TeamID == source.TeamID);
                    if (team != null)
                    {
                        team.Name = String.IsNullOrEmpty(source.Name) ? null: source.Name;
                        team.Note = String.IsNullOrEmpty(source.Note) ? null : source.Note;
                        
                        data.SubmitChanges();

                        return true;
                    }
                    else
                    {
                        ErrorMessage = Common.Constant.SqlErrorMessage.NOT_EXIST_OBJECT_IN_DB;

                        return false;
                    }
                }
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;

                return false;
            }
        }

        public int? GetCount()
        {
            try
            {
                using (var data = new DataBaseDataContext())
                {
                    return data.Teams.Count();
                }
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;

                return null;
            }
        }

        public IEnumerable<Entities.Team> GetAll(int start, int limit)
        {
            try
            {
                using (var data = new DataBaseDataContext())
                {
                    var obj = data.Teams.OrderBy(p => p.Name).Skip(start).Take(limit).ToList();
                    var result = (from p in obj
                                  select new Entities.Team()
                                  {
                                      TeamID = p.TeamID,
                                      Name = String.IsNullOrEmpty(p.Name) ? string.Empty : p.Name,
                                      Note = String.IsNullOrEmpty(p.Note) ? string.Empty : p.Note
                                  }).ToList();

                    return result;
                }
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;

                return null;
            }
        }

        public IEnumerable<Entities.Team> FilterByName(int start, int limit, string key, out int total)
        {
            try
            {
                using (var data = new DataBaseDataContext())
                {
                    var obj = data.Teams.Where(p => p.Name.Contains(key)).OrderBy(p => p.Name);
                    total = obj.Count();
                    var result = (from p in obj.Skip(start).Take(limit).ToList()
                                  select new Entities.Team()
                                  {
                                      TeamID = p.TeamID,
                                      Name = String.IsNullOrEmpty(p.Name) ? string.Empty : p.Name,
                                      Note = String.IsNullOrEmpty(p.Note) ? string.Empty : p.Note
                                  }).ToList();

                    return result;
                }
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;
                total = -1;
                return null;
            }
        }

        public Entities.Team GetTeam(int teamID)
        {
            try
            {
                using (var data = new DataBaseDataContext())
                {
                    var result = (from p in data.Teams
                                  where p.TeamID == teamID
                                  select new Entities.Team
                                  {
                                      TeamID = p.TeamID,
                                      Name = p.Name,
                                      Note = p.Note
                                  }).FirstOrDefault();

                    return result;
                }
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;

                return null;
            }
        }
    }
}
